<template>
     <h2 style="text-align: center;">用户理财赎回</h2>
     <table>
          <tr>
            <td>理财产品名称</td>
            <td>{{info.F_name}}</td>
          </tr>
          <tr>
            <td>年收益</td>
            <td>{{info.F_APY}}</td>
          </tr>
          <tr>
            <td>最低理财金额</td>
            <td>{{info.F_MinMoney}}</td>
          </tr>
          <tr>
            <td>风险等级</td>
            <td>{{info.F_Level==0?"低风险":info.F_Level==1?"中风险":"高风险"}}</td>
          </tr>
          <tr>
            <td>用户购买金额</td>
            <td>{{info.UF_Money}}</td>
          </tr>
          <tr>
            <td>赎回金额</td>
            <td><input type="text"  v-model="UFR_Money"></td>
          </tr>
          <tr>
            <td colspan="2"><button @click="SaveData">提交</button></td>
          </tr>
     </table>
</template>

<script setup lang="ts">
import {useRoute,useRouter} from 'vue-router'

const router = useRouter();
import axios from 'axios'

const route = useRoute()

import {ref,onMounted} from 'vue';

alert(route.params.UFid)

const info = ref({
    F_name:"",
    F_APY:"",
    F_MinMoney:"",
    F_Level:0,
    UF_Money:""
})

onMounted(()=>{
    LoadData();
})

const LoadData = () =>{
    axios({
       url:"https://localhost:44337/api/Finans/GetUserFinanInfo",
       method:"get",
       params:{
           UFid:route.params.UFid
       }
    }).then((res)=>{
        console.log(res.data)
        info.value = res.data[0]
    }).catch((err)=>{
        console.log(err)
    })
}

//赎回金额
const UFR_Money = ref("")

//完成确认赎回功能
const SaveData =()=>{
    //赎回金额需大于0元
    if(Number(UFR_Money.value)<=0){
        alert("赎回金额需大于0元")
        return;
    }

    //赎回金额需小于等于用户购买金额
    if(UFR_Money.value>info.value.UF_Money){
        alert("赎回金额需小于等于用户购买金额")
        return;
    }

    axios({
        url:"https://localhost:44337/api/Finans/AddUserFinanRedeemInfo",
        method:"post",
        data:{
              UF_Id:route.params.UFid,
              UFR_Money:UFR_Money.value
        }
    }).then(res=>{
        console.log(res.data)
        if(res.data >0){
            alert("赎回成功")
        }else{
            alert("赎回失败")
        }
        router.push('/ShowFinanRemdeem')
    }).catch(err=>{
        console.log(err)
    })
}

</script>